package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"task-server-gin/client"
	"task-server-gin/dao"
	"task-server-gin/model"
	"task-server-gin/utils"
)

type TaskController struct {
}

func changeTaskRecord(task *model.GormSingleTask) {
	fmt.Printf("changeTaskRecord: %#v ", task)
	taskId := task.TaskId
	record := client.MysqlTask{}.UpdateTaskRecord(taskId)
	fmt.Printf("changeTaskRecord: %#v ", record)
	if record.Release == 0 {
		fmt.Println("task id=", taskId, "已经完成所有搜索任务，发送邮件通知用户")
		utils.Email{}.Notify(record.Email, "")
	}
}

func (con TaskController) GetFreshTask(c *gin.Context) {
	result, err := dao.MysqlDao{}.GetSingleTask() // task *model.GormSingleTask
	if len(result.Event) == 0 {
		c.JSON(201, model.Resp{}.Empty(&[]string{}))
		return
	}
	if err != nil {
		c.JSON(200, model.Resp{}.Error(result))
		return
	}
	go changeTaskRecord(result)
	c.JSON(200, model.Resp{}.Ok(result))
}

func (con TaskController) Connect(c *gin.Context) {
	err := client.InitDB()
	if err != nil {
		c.String(500, err.Error())
		return
	}
	c.String(200, "success connect!")
}

func (con TaskController) UpdateTaskState(c *gin.Context) {
	jobId := c.Query("id")
	fmt.Println(jobId)
	result := dao.MysqlDao{}.UpdateTaskStateById(jobId)
	if result != nil {
		c.JSON(200, model.Resp{}.Ok("更新成功"))
		return
	}
	c.JSON(200, model.Resp{Code: 201}.Empty("没有找到对应的id/已经更新过了"))
}

func (con TaskController) GetDownloadRecord(c *gin.Context) {
	url := c.DefaultPostForm("url", "nil")
	if url == "nil" {
		fmt.Println("没有获取到url")
		c.JSON(500, model.Resp{Code: 500}.Error("没有获取到url"))
		return
	}
	result, err := dao.MysqlDao{}.GetDownloadRecord(url)
	if err != nil {
		fmt.Println("GetDownloadRecord catch error=", err)
		c.JSON(200, model.Resp{}.Empty(fmt.Sprintf("查询遇到问题了：%#v", err)))
		return
	}
	if result {
		c.JSON(200, model.Resp{}.Empty("没有采集过"))
		return
	}
	c.JSON(200, model.Resp{}.Scrapyed("采集过了"))
}

func (con TaskController) InsertDownloadRecord(c *gin.Context) {
	url := c.DefaultPostForm("url", "nil")
	fmt.Println("InsertDownloadRecord url =", url)
	result, err := dao.MysqlDao{}.InsertDownloadRecord(url)
	fmt.Println(result, err)
	if err != nil {
		c.JSON(200, model.Resp{Code: 200}.Error("以前添加过了"))
		return
	}
	c.JSON(200, model.Resp{
		Code:    200,
		Message: "success",
	}.Ok("添加成功"))
}

func (con TaskController) InsertVideoInfo(c *gin.Context) {
	responseJson := model.VideoInfo{}
	err := c.BindJSON(&responseJson)
	fmt.Printf("InsertVideoInfo.responseJson =%#v", responseJson)
	if err == nil {
		result, err := client.MysqlTask{}.InsertVideoInfo(&responseJson)
		if err != nil {
			c.JSON(500, model.Resp{}.Error(err))
			return
		}
		c.JSON(200, model.Resp{}.Ok(result))
		return
	}
	c.JSON(500, model.Resp{}.Error(err))
}

func (con TaskController) InsertAuthorInfo(c *gin.Context) {
	responseJson := model.AuthorInfo{}
	err := c.BindJSON(&responseJson)
	fmt.Printf("InsertAuthorInfo.responseJson =%#v", responseJson)
	if err == nil {
		result, err := client.MysqlTask{}.InsertAuthorInfo(&responseJson)
		if err != nil {
			c.JSON(500, model.Resp{}.Error(err))
			return
		}
		c.JSON(200, model.Resp{}.Ok(result))
		return
	}
	c.JSON(500, model.Resp{}.Error(err))
}
